import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../models/order.dart';
import '../models/order_item.dart';
import '../models/delivery_info.dart';
import 'base-service.dart';

class OrderService extends BaseService {
  OrderService({required Ref ref}) : super(ref: ref);

  // 获取订单列表
  Future<Map<String, dynamic>> getOrderList({
    String? status,
    String? openid,
    int page = 1,
    int pageSize = 20,
  }) async {
    try {
      final queryParams = <String, dynamic>{
        'page': page,
        'pageSize': pageSize,
      };
      
      if (status != null && status.isNotEmpty) {
        queryParams['status'] = status;
      }
      if (openid != null && openid.isNotEmpty) {
        queryParams['openid'] = openid;
      }

      final response = await get<Map<String, dynamic>>(
        '/admin/order/get-list',
        queryParams: queryParams,
      );

      if (response['success']) {
        return response['data'];
      } else {
        throw Exception(response['message'] ?? '获取订单列表失败');
      }
    } catch (e) {
      throw Exception('获取订单列表失败: $e');
    }
  }

  // 获取订单详情
  Future<Order> getOrderById(String id) async {
    try {
      final response = await get<Map<String, dynamic>>('/admin/order/$id');

      if (response['success']) {
        return Order.fromJson(response['data']);
      } else {
        throw Exception(response['message'] ?? '获取订单详情失败');
      }
    } catch (e) {
      throw Exception('获取订单详情失败: $e');
    }
  }

  // 创建订单
  Future<String> createOrder(Map<String, dynamic> orderData) async {
    try {
      final response = await post<Map<String, dynamic>>(
        '/admin/order/create',
        body: orderData,
      );

      if (response['success']) {
        return response['data']['id'];
      } else {
        throw Exception(response['message'] ?? '创建订单失败');
      }
    } catch (e) {
      throw Exception('创建订单失败: $e');
    }
  }

  // 更新订单
  Future<void> updateOrder(String id, Map<String, dynamic> orderData) async {
    try {
      final response = await put<Map<String, dynamic>>(
        '/admin/order/$id',
        body: orderData,
      );

      if (!response['success']) {
        throw Exception(response['message'] ?? '更新订单失败');
      }
    } catch (e) {
      throw Exception('更新订单失败: $e');
    }
  }

  // 更新订单状态
  Future<void> updateOrderStatus(String id, String status) async {
    try {
      final response = await put<Map<String, dynamic>>(
        '/admin/order/$id/status',
        body: {'status': status},
      );

      if (!response['success']) {
        throw Exception(response['message'] ?? '更新订单状态失败');
      }
    } catch (e) {
      throw Exception('更新订单状态失败: $e');
    }
  }

  // 删除订单
  Future<void> deleteOrder(String id) async {
    try {
      final response = await delete<Map<String, dynamic>>('/admin/order/$id');

      if (!response['success']) {
        throw Exception(response['message'] ?? '删除订单失败');
      }
    } catch (e) {
      throw Exception('删除订单失败: $e');
    }
  }
}

class OrderItemService extends BaseService {
  OrderItemService({required Ref ref}) : super(ref: ref);

  // 获取订单项列表
  Future<List<OrderItem>> getOrderItemList({
    String? orderId,
    String? skuId,
  }) async {
    try {
      final queryParams = <String, dynamic>{};
      
      if (orderId != null && orderId.isNotEmpty) {
        queryParams['orderId'] = orderId;
      }
      if (skuId != null && skuId.isNotEmpty) {
        queryParams['skuId'] = skuId;
      }

      final response = await get<Map<String, dynamic>>(
        '/admin/order-item/get-list',
        queryParams: queryParams,
      );

      if (response['success']) {
        final List<dynamic> data = response['data'];
        return data.map((item) => OrderItem.fromJson(item)).toList();
      } else {
        throw Exception(response['message'] ?? '获取订单项列表失败');
      }
    } catch (e) {
      throw Exception('获取订单项列表失败: $e');
    }
  }

  // 获取订单项详情
  Future<OrderItem> getOrderItemById(String id) async {
    try {
      final response = await get<Map<String, dynamic>>('/admin/order-item/$id');

      if (response['success']) {
        return OrderItem.fromJson(response['data']);
      } else {
        throw Exception(response['message'] ?? '获取订单项详情失败');
      }
    } catch (e) {
      throw Exception('获取订单项详情失败: $e');
    }
  }

  // 创建订单项
  Future<String> createOrderItem(Map<String, dynamic> orderItemData) async {
    try {
      final response = await post<Map<String, dynamic>>(
        '/admin/order-item/create',
        body: orderItemData,
      );

      if (response['success']) {
        return response['data']['id'];
      } else {
        throw Exception(response['message'] ?? '创建订单项失败');
      }
    } catch (e) {
      throw Exception('创建订单项失败: $e');
    }
  }

  // 更新订单项
  Future<void> updateOrderItem(String id, Map<String, dynamic> orderItemData) async {
    try {
      final response = await put<Map<String, dynamic>>(
        '/admin/order-item/$id',
        body: orderItemData,
      );

      if (!response['success']) {
        throw Exception(response['message'] ?? '更新订单项失败');
      }
    } catch (e) {
      throw Exception('更新订单项失败: $e');
    }
  }

  // 删除订单项
  Future<void> deleteOrderItem(String id) async {
    try {
      final response = await delete<Map<String, dynamic>>('/admin/order-item/$id');

      if (!response['success']) {
        throw Exception(response['message'] ?? '删除订单项失败');
      }
    } catch (e) {
      throw Exception('删除订单项失败: $e');
    }
  }

  // 获取指定订单的订单项列表
  Future<List<OrderItem>> getOrderItemsByOrderId(String orderId) async {
    try {
      final response = await get<Map<String, dynamic>>('/admin/order-item/order/$orderId');

      if (response['success']) {
        final List<dynamic> data = response['data'];
        return data.map((item) => OrderItem.fromJson(item)).toList();
      } else {
        throw Exception(response['message'] ?? '获取订单项列表失败');
      }
    } catch (e) {
      throw Exception('获取订单项列表失败: $e');
    }
  }
}

class DeliveryInfoService extends BaseService {
  DeliveryInfoService({required Ref ref}) : super(ref: ref);

  // 获取收货信息列表
  Future<List<DeliveryInfo>> getDeliveryInfoList({String? openid}) async {
    try {
      final queryParams = <String, dynamic>{};
      
      if (openid != null && openid.isNotEmpty) {
        queryParams['openid'] = openid;
      }

      final response = await get<Map<String, dynamic>>(
        '/admin/delivery-info/get-list',
        queryParams: queryParams,
      );

      if (response['success']) {
        final List<dynamic> data = response['data'];
        return data.map((item) => DeliveryInfo.fromJson(item)).toList();
      } else {
        throw Exception(response['message'] ?? '获取收货信息列表失败');
      }
    } catch (e) {
      throw Exception('获取收货信息列表失败: $e');
    }
  }

  // 获取收货信息详情
  Future<DeliveryInfo> getDeliveryInfoById(String id) async {
    try {
      final response = await get<Map<String, dynamic>>('/admin/delivery-info/$id');

      if (response['success']) {
        return DeliveryInfo.fromJson(response['data']);
      } else {
        throw Exception(response['message'] ?? '获取收货信息详情失败');
      }
    } catch (e) {
      throw Exception('获取收货信息详情失败: $e');
    }
  }

  // 创建收货信息
  Future<String> createDeliveryInfo(Map<String, dynamic> deliveryInfoData) async {
    try {
      final response = await post<Map<String, dynamic>>(
        '/admin/delivery-info/create',
        body: deliveryInfoData,
      );

      if (response['success']) {
        return response['data']['id'];
      } else {
        throw Exception(response['message'] ?? '创建收货信息失败');
      }
    } catch (e) {
      throw Exception('创建收货信息失败: $e');
    }
  }

  // 更新收货信息
  Future<void> updateDeliveryInfo(String id, Map<String, dynamic> deliveryInfoData) async {
    try {
      final response = await put<Map<String, dynamic>>(
        '/admin/delivery-info/$id',
        body: deliveryInfoData,
      );

      if (!response['success']) {
        throw Exception(response['message'] ?? '更新收货信息失败');
      }
    } catch (e) {
      throw Exception('更新收货信息失败: $e');
    }
  }

  // 删除收货信息
  Future<void> deleteDeliveryInfo(String id) async {
    try {
      final response = await delete<Map<String, dynamic>>('/admin/delivery-info/$id');

      if (!response['success']) {
        throw Exception(response['message'] ?? '删除收货信息失败');
      }
    } catch (e) {
      throw Exception('删除收货信息失败: $e');
    }
  }

  // 获取用户的收货信息列表
  Future<List<DeliveryInfo>> getDeliveryInfoByUser(String openid) async {
    try {
      final response = await get<Map<String, dynamic>>('/admin/delivery-info/user/$openid');

      if (response['success']) {
        final List<dynamic> data = response['data'];
        return data.map((item) => DeliveryInfo.fromJson(item)).toList();
      } else {
        throw Exception(response['message'] ?? '获取用户收货信息失败');
      }
    } catch (e) {
      throw Exception('获取用户收货信息失败: $e');
    }
  }
} 